//microcommand define oeis.search
signature("<query:rawtext>", { strictOptions: true })
option("index", "-i <natural:natural>", { fallback: 1 })
before(({ session, options }, query) => {
  if (!options.help && !query) return session.i18n(".expect-query")
})
action(async ({ session, options }, query) => {
  const params = {
    q: query,
    fmt: "json",
    n: 2,
    start: options.index - 1,
  }
  const a = await ctx.http.get("https://oeis.org/search", { params, responseType: "json" })
  if (!a) {
    params.fmt = "text"
    const textResp = await ctx.http.get("https://oeis.org/search", { params, responseType: "text" })
    return session.i18n(textResp.includes("Too many results.") ? ".too-many-results" : ".no-result")
  }
  const [r, r2] = a
  const aNumber = "A" + r.number.toString().padStart(6, "0")
  let keywords = ""
  if (r.keyword) {
    const a = r.keyword.split(",")
    keywords = session.i18n([`.keyword.${a[0]}`, ""])
    if (!keywords.length) keywords.push(a[0])
    for (const w of a.slice(1)) {
      let t = session.i18n([`.keyword.${w}`, ""])
      if (!t.length) t = [w]
      keywords.push(...session.i18n("general.comma"), ...t)
    }
  }
  const result = session.i18n(".result", {
    ...r,
    aNumber,
    url: `https://oeis.org/${aNumber}`,
    keywords,
  })
  if (r2) result.push(h("p", session.i18n(".next", [`${name.replaceAll(".", " ")} -i ${options.index + 1} ${query}`])))
  return result
})
locale("zh-CN", {
  description: "搜索整数数列在线百科全书",
  messages: {
    "no-result": "未找到符合条件的数列…",
    "too-many-results": "符合条件的数列过多，请缩小搜索范围…",
    result: "{aNumber}: {name}\n{data}\n{#if formula[0]}公式：{formula[0]}\n{#if formula[1]}……\n{/if}{/if}{#if keywords}标签：{keywords}\n{/if}详见：{url}",
    next: "使用 {0} 查看下一条结果",
    keyword: {
      allocated: "待编（allocated）",
      allocating: "分配中（allocating）",
      base: "进制相关（base）",
      bref: "条目过少（bref）",
      changed: "近期修改（changed）",
      cofr: "连分数展开（cofr）",
      cons: "小数展开（cons）",
      core: "重要（core）",
      dead: "冻结（dead）",
      dumb: "低价值（dumb）",
      easy: "易计算（easy）",
      eigen: "变换不变（eigen）",
      fini: "有限（fini）",
      frac: "分数分子（frac）",
      full: "完整列出（full）",
      hard: "难计算（hard）",
      hear: "听觉优化（hear）",
      less: "低关注度（less）",
      look: "视觉优化（look）",
      more: "需补充项（more）",
      mult: "积性函数（mult）",
      new: "新增（new）",
      nice: "精选（nice）",
      nonn: "非负（nonn）",
      obsc: "定义模糊（obsc）",
      probation: "待审核（probation）",
      recycled: "回收（recycled）",
      sign: "含负项（sign）",
      tabf: "不规则表（tabf）",
      tabl: "规则表（tabl）",
      uned: "待润色（uned）",
      unkn: "未知定义（unkn）",
      walk: "路径计数（walk）",
      word: "语言相关（word）",
    },
  },
})